package com.siyao.redvip.ui.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.http.RequestManager;
import com.android.volley.http.RequestManager.RequestListener;
import com.google.gson.reflect.TypeToken;
import com.siyao.redvip.R;
import com.siyao.redvip.RedApplication;
import com.siyao.redvip.bean.Comment;
import com.siyao.redvip.bean.CommentBean;
import com.siyao.redvip.bean.FriendCircle;
import com.siyao.redvip.bean.FriendCircleBean;
import com.siyao.redvip.bean.ResponseTBean;
import com.siyao.redvip.bean.TResponseBean;
import com.siyao.redvip.components.base.BaseFragmentActivity;
import com.siyao.redvip.components.emojicon.EmoteInputView;
import com.siyao.redvip.components.emojicon.EmoticonsEditText;
import com.siyao.redvip.components.view.MoreImageView;
import com.siyao.redvip.image.ImageLoaderOptions;
import com.siyao.redvip.network.NetConstans;
import com.siyao.redvip.ui.adapter.CommentAdapter;
import com.siyao.redvip.utils.AppUtils;
import com.siyao.redvip.utils.ToastUtils;
import com.siyao.redvip.utils.Track;

/**
 * 朋友圈详情
 * @author lenovo
 *
 */
public class FriendDetailsActivity extends BaseFragmentActivity {

	private Button btn_user_comment_emo;// 表情按钮
	
	private EmoticonsEditText edit_user_comment;// 显示输入的文字
	
	private TextView txtbtn_user_comment_send;// 发送评论

	private EmoteInputView mInputView;// 表情布局
	
	ImageView	iv_user_icon;//发布者的头像
	TextView tv_username;//发布者的名字
	TextView tv_time;//时间
	LinearLayout linear_layout;
	
	LinearLayout comment_layout;//评论的布局
	
	MoreImageView more_image;//发的照片
	
	ListView list_view_replay;//评论列表
	
	FriendCircle  friendCircle;
	CommentAdapter commentAdapter;
	List<Comment> list;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleText("评论活动");
		setLeftButton("返回");
	}
	
	@Override
	protected void beforeInit() {
		 friendCircle=(FriendCircle) getIntent().getSerializableExtra("friendCircle");
	}

	@Override
	public void ininlazeLayout() {
		View main =getLayoutInflater().inflate(R.layout.activity_friend_detail_fragment, null);
		appMainView.addView(main, layoutParams);
	}

	@Override
	public void ininlazeView() {
		
		linear_layout =  (LinearLayout) findViewById(R.id.linear_layout);
		iv_user_icon = (ImageView) findViewById(R.id.iv_user_icon);
		tv_username = (TextView) findViewById(R.id.tv_username);
		tv_time = (TextView) findViewById(R.id.tv_time);
		more_image = (MoreImageView) findViewById(R.id.more_image);
		list_view_replay =(ListView) findViewById(R.id.list_view_replay);
		
		
		btn_user_comment_emo = (Button) findViewById(R.id.btn_user_comment_emo);
		edit_user_comment = (EmoticonsEditText) findViewById(R.id.edit_user_comment);
		txtbtn_user_comment_send = (TextView) findViewById(R.id.btn_user_comment_send);
		mInputView = (EmoteInputView) findViewById(R.id.chat_eiv_inputview);
		mInputView.setEditText(edit_user_comment);
		btn_user_comment_emo.setOnClickListener(this);
		txtbtn_user_comment_send.setOnClickListener(this);
		linear_layout.setOnClickListener(this);
		
		edit_user_comment.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mInputView.setVisibility(View.GONE);
                return false;
            }
        });
		
		mInputView.setOnFocusChangeListener(new OnFocusChangeListener() {
	            @Override
	            public void onFocusChange(View arg0, boolean arg1) {
	                hideSoftInputView(FriendDetailsActivity.this);
	            }
	        });
		
		edit_user_comment.addTextChangedListener(new TextWatcher() {
            CharSequence inputTemp = "";
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                inputTemp = s;
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
            	
            }
        });
		commentAdapter = new CommentAdapter(this);
	}

	@Override
	public void ininlazeData() {
		showPd();
		imageLoader.displayImage(friendCircle.getHeadPortrait(), iv_user_icon, ImageLoaderOptions.getHeaderOptions());
		tv_username.setText(friendCircle.getName());
		tv_time.setText(friendCircle.getTime());
		String[] images= friendCircle.getImages().split(",");
		List<String>paths = new ArrayList<String>();
		for (int i = 0; i < images.length; i++) {
			String string = images[i];
			paths.add(string);
		}
		more_image.setPaths(paths);
		String url=NetConstans.FRIEND_COMMENTLIST+"?recordId="+friendCircle.getRecordId();
		RequestManager.getInstance(this).get(url, new RequestListener() {
			
			@Override
			public void onSuccess(String response, String url, int actionId) {
				dismissPd();
				Track.i(response);
				FriendCircleBean friendCircle=gson.fromJson(response, new TypeToken<FriendCircleBean>() {}.getType());
				list =friendCircle.getData().getCommentList();
				if(list.isEmpty()){
					list_view_replay.setVisibility(View.INVISIBLE);
				}else{
					commentAdapter.setComments(list);
					list_view_replay.setAdapter(commentAdapter);
				}
			}
			
			@Override
			public void onRequest() {
				
			}
			
			@Override
			public void onError(String errorMsg, String url, int actionId) {
				dismissPd();
				ToastUtils.makeMesageShort(FriendDetailsActivity.this, getString(R.string.server_error));
			}
		}, 1);
	}

	@Override
	public void onClick(View v) {
		int id =v.getId();
		switch (id) {
		case R.id.top_left_btn:
			Intent intent = new Intent(this, FriendActivity.class);
			startActivity(intent);
			finish();
			break;
		case R.id.btn_user_comment_emo:
			if (View.VISIBLE == mInputView.getVisibility()) {
                mInputView.setVisibility(View.GONE);
            } else {
                hideSoftInputView(this);
                edit_user_comment.setFocusable(true);   
    			edit_user_comment.setFocusableInTouchMode(true);   
    			edit_user_comment.requestFocus();
                showEmotionView1();
            }
			break;
		case R.id.edit_user_comment:
			hideSoftInputView(this);
			edit_user_comment.setFocusable(true);   
			edit_user_comment.setFocusableInTouchMode(true);   
			edit_user_comment.requestFocus();
			break;
		case R.id.linear_layout:
			edit_user_comment.clearFocus();
			AppUtils.hideKeyBorad(this, edit_user_comment);
			break;
		case R.id.btn_user_comment_send:
			doReplyAction();
			break;
		default:
			break;
		}
	}
	
	//评论
	private void doReplyAction(){
		String content = edit_user_comment.getText().toString();
		if(content!=null && TextUtils.isEmpty(content)){
			ToastUtils.makeMesageShort(this, "请输入评论内容");
			return;
		}
		showPd();
		int recordId=friendCircle.getRecordId();
		String volunteerId=RedApplication.getUser().getVolunteerId();
		
		Map<String,String> params = new HashMap<String,String>();
		params.put("volunteerId", volunteerId);
		params.put("recordId", recordId+"");
		params.put("content", content);
		RequestManager.getInstance(this).post(NetConstans.ADD_COMMENTLIST, params, new RequestListener() {
			
			@Override
			public void onSuccess(String response, String url, int actionId) {
				dismissPd();
				Track.i(response);
				list_view_replay.setVisibility(View.VISIBLE);
				list.clear();
				CommentBean bean=gson.fromJson(response, new TypeToken<CommentBean>() {}.getType());
				if(bean.getStatus()!=0){
					list=bean.getComments();
					commentAdapter.setComments(list);
					list_view_replay.setAdapter(commentAdapter);
				}
				edit_user_comment.setText("");
				AppUtils.hideKeyBorad(FriendDetailsActivity.this, edit_user_comment);
			}
			
			@Override
			public void onRequest() {
				
			}
			
			@Override
			public void onError(String errorMsg, String url, int actionId) {
				dismissPd();
			}
		}, 2);
	}
	private void hideSoftInputView(Context context) {
		if (context == null)
			return;
		InputMethodManager manager = ((InputMethodManager) context
				.getSystemService(Activity.INPUT_METHOD_SERVICE));
		View view = ((Activity) context).getCurrentFocus();
		if (view == null)
			return;
		manager.hideSoftInputFromWindow(view.getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}

	/**
	 * 显示表情布局 showEmotionView
	 * 
	 * @throws
	 */
	private void showEmotionView1() {
		mInputView.setVisibility(View.VISIBLE);
	}
	
    private void resetInputBar() {
        edit_user_comment.setText("");
        edit_user_comment.setHint("发布评论");
        mInputView.setVisibility(View.GONE);
        com.siyao.redvip.utils.AppUtils.showKeyBorad(this, edit_user_comment);
    }
}
